<?php defined('BASEPATH') OR exit('No direct script access allowed');
/**
 * Description: 系统设置
 *
 * @author      Eden
 * @datetime    2016/11/26 21:43
 * @copyright   Beijing CmsTop Technology Co.,Ltd.
 */

class Setting extends ADM_Controller
{

    // 最大上传尺寸 200KB
    const MAX_UPLOAD_SIZE = 200 * 1024;

    /**
     * 初始化
     */
    public function __construct()
    {
        parent::__construct();
    }

    public function index()
    {
        $this->load->view(parent::TMP_DIR . '/system/setting.html');
    }

    /**
     * 获取设置
     */
    public function getSets()
    {
        $this->load->model('admin/system/Setting_Model', 'setting');
        $settings = $this->setting->get('system', array('baseSetting', 'contact'));
        if ($settings) {
            $send['state'] = true;
            $settings['baseSetting'] = array_merge($settings['baseSetting'],
                array('fullPath' => thumb($settings['baseSetting']['logo'], 190, 44)));
            $send['data'] = $settings;
        } else {
            $send['state'] = false;
            $send['error'] = '配置获取失败';
        }
        $this->output->send($send);
    }

    /**
     * 保存基础设置
     */
    public function saveBaseSetting()
    {
        $send['state'] = false;
        do {

            if ($this->input->method(true) !== 'POST') {
                $send['error'] = '非法请求';
                break;
            }

            $params = $this->input->post();
            if (!empty($params)) foreach($params as $key => $param) {
                if (!in_array($key, array('logo', 'siteName', 'beian', 'copyright'))) {
                    $send['error'] = '包含无效字段';
                    break 2;
                }
            }

            $this->load->model('admin/system/Setting_Model', 'setting');
            if ($res = $this->setting->save('baseSetting', $params)) {
                $send['state'] = true;
            } else {
                $send['error'] = '保存失败';
            }

        } while(false);
        $this->output->send($send);
    }

    /**
     * 上传图片 Logo
     */
    public function uploadLogo()
    {
        $send['state'] = false;
        do {
            if ($this->input->method(true) !== 'POST') {
                $send['error'] = '非法请求';
                break;
            }

            if (empty($_FILES['file'])) {
                $send['error'] = '图片不合法';
                break;
            }

            if (!empty($_FILES['file']['size']) && $_FILES['file']['size'] > self::MAX_UPLOAD_SIZE) {
                $send['error'] = '图片尺寸太大';
                break;
            }

            $res = local($_FILES['file']);
            if (empty($res)) {
                $send['error'] = '上传失败';
            } else {
                $send['state'] = true;
                $send['data'] = array_merge($res, array('fullPath' => thumb($res['key'], 190, 44)));
            }
        } while(false);
        $this->output->send($send);
    }

    /**
     * 保存联系方式
     */
    public function setContact()
    {
        $send['state'] = false;
        do {

            if ($this->input->method(true) !== 'POST') {
                $send['error'] = '非法请求';
                break;
            }

            $params = $this->input->post();
            if (!empty($params)) foreach($params as $key => $param) {
                if (!in_array($key, array('address', 'qq', 'telphone', 'phone', 'zipCode', 'email'))) {
                    $send['error'] = '包含无效字段';
                    break 2;
                }
            }

            if (!empty($params['email']) && !filter_var($params['email'], FILTER_VALIDATE_EMAIL)) {
                $send['error'] = '电子邮箱格式无效';
                break;
            }

            if (!empty($params['qq']) && !preg_match('/^\d{5,12}$/', $params['qq'])) {
                $send['error'] = 'QQ号格式错误';
                break;
            }

            if (!empty($params['phone']) && !preg_match('/^1[34578]\d{9}$/', $params['phone'])) {
                $send['error'] = '手机号格式错误';
                break;
            }

            if (!empty($params['zipCode']) && !preg_match('/^[1-9][0-9]{5}$/', $params['zipCode'])) {
                $send['error'] = '邮编格式错误';
                break;
            }

            if (!empty($params['telphone']) && !preg_match('/^0\d{2,3}-?\d{7,8}$/', $params['telphone'])
                && !preg_match('/^400-([0-9]){1}([0-9-]{7})$/', $params['telphone'])) {
                $send['error'] = '电话号码格式错误';
                break;
            }

            $this->load->model('admin/system/Setting_Model', 'setting');
            if ($res = $this->setting->save('contact', $params)) {
                $send['state'] = true;
            } else {
                $send['error'] = '保存失败';
            }

        } while(false);
        $this->output->send($send);
    }
}
